package InvestmentPojo;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
	/**
	 * -----------------------------------------------------------------------------
	 * Used to provide an example of how to get/modify/save Properties files
	 * while preserving comments in the file.
	 * -----------------------------------------------------------------------------
	 */
	 
public class UpdatePropertiesFile extends Action{
	private static final ActionForward GET_ANSWER = null;
	private static final ActionForward UPDATE_ANSWER = null;
	private static final ActionForward SUCCESS = null;


	public ActionForward execute(ActionMapping mapping, ActionForm inForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
	
			String  forward = "SUCCESS";
			String formId= "3";
			formId= request.getParameter("formId");
			if(formId != null && formId != ""){
			final String PROPFILE= "D:/New_Fema_Project/WebContent/WEB-INF/classes/ApplicationResources_Form_"+formId+".properties";
	        Properties myProp;
	        Properties myNewProp;
	        String key2 = "";
	        String value = "";
	        String methodname="";
	        String str = new String();
	        str = "<font color=red>No file found to update...</font>";
	        methodname= request.getParameter("method").trim();
	        key2= request.getParameter("Key").trim();
	        
	        if(null != methodname && "GET_ANSWER".equals(methodname)){
	        // Input Properties File
	        myProp = loadProperties(PROPFILE);
	        if(key2 !=null && key2 != ""){
	        String answer = myProp.getProperty(key2);
	        //str = answer;
	        if(str != null && str != "")
	        	str="";
	        response.setContentType("text/html;charset=utf-8");
		    response.setHeader("cache-control", "no-cache");
		    if(answer != null && answer != ""){
		    //response.getWriter().write(str);
		    request.setAttribute("Answer",answer);
		    request.setAttribute("key",key2);
	        }else{
	        	str = "<font color=red>Invalid tool name or key...<font>";
		        request.setAttribute("Result",str);
	        }
	        }
	        //forward =GET_ANSWER;
	        }
	        	
	        if(null != methodname && "UPDATE_ANSWER".equals(methodname)){
	        // Modified Properties File
	        	value= request.getParameter("Value").trim();
	        myProp = loadProperties(PROPFILE);
	        if(key2 !=null && key2 != "" && value != null && value != ""){
	        myNewProp = alterProperties(myProp,key2,value);
	        saveProperties(myNewProp, PROPFILE);
	        str="<font color=green>Updated Successfully...</font>";
	        request.setAttribute("Result",str);
	        response.setContentType("text/html;charset=utf-8");
		    response.setHeader("cache-control", "no-cache");
		    if(str != null && str != "")
		    response.getWriter().write(str);
	        }else{
	        	str = "<font color=red>Invalid key or answer...</font>";
		        request.setAttribute("Result",str);
	        }
	        //forward =UPDATE_ANSWER;
	        }
	        request.setAttribute("Result",str);
	        request.setAttribute("formId", formId);
		    
			}
			return mapping.findForward(forward);
	}
	    public static void printProperties(Properties p, String s) {
	 
	        System.out.println();
	        System.out.println();
	 
	        System.out.println(s);
	 
	        System.out.println();
	 
	        p.list(System.out);
	        System.out.println();
	    }
	 
	 
	   public static void saveProperties(Properties modifiedProp, String fileName) {
	 
	        OutputStream outPropFile;
	 
	        try {
	        	   outPropFile=new FileOutputStream(fileName);
				   modifiedProp.save(outPropFile, null);
				   outPropFile.close();
	        } catch (IOException ioe) {
	            System.out.println("I/O Exception.");
	            ioe.printStackTrace();
	            System.exit(0);
	        }
	 
	    }
	 
	 
	    public static Properties loadProperties(String fileName) {
	 
	        InputStream inPropFile;
	        Properties tempProp = new Properties();
	 
	        try {
	            inPropFile = new FileInputStream(fileName);
	            tempProp.load(inPropFile);
	            inPropFile.close();
	        } catch (IOException ioe) {
	            System.out.println("I/O Exception.");
	            ioe.printStackTrace();
	            System.exit(0);
	        }
	 
	        return tempProp;
	 
	    }
	 
	 
	    public static Properties alterProperties(Properties prop,String key,String value) {
	    	Map propPreviousMap = new HashMap(prop);
			Map propPresentMap = new HashMap();
			propPresentMap = propPreviousMap;
			
			if(propPresentMap.containsKey(key)){
				propPresentMap.put(key, value);
			}else{
				propPresentMap.put(key, value);
			}
			
			 Properties modifiedProp = new Properties();
			 OutputStream outPropFile;
			   Set<Map.Entry<String,String>> set = propPresentMap.entrySet();
			   for (Map.Entry<String,String> entry : set) {
				   modifiedProp.put(entry.getKey(), entry.getValue());
			   }
			   
	        return modifiedProp;
	 
	    }
	 
	 
	
	}